package com.sky.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    DishMapper dishMapper;

    @Autowired
    DishFlavorMapper dishFlavorMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    SetmealDishMapper setmealDishMapper;

    /**
     * 保存菜品和口味信息
     * @param dishDTO
     */
    @Transactional
    @Override
    public void saveDishAndFlavors(DishDTO dishDTO) {
        //1.保存菜品
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //注意:我们应该返回主键
        dishMapper.save(dish);
        //2.保存口味
        //这里有个深坑!!!
        //存口味是需要和菜品的id绑定
        //我们需要给每个口味设置dishId
        List<DishFlavor> flavorList = dishDTO.getFlavors()
                .stream()
                .map(dishFlavor -> {
                    dishFlavor.setDishId(dish.getId());
                    return dishFlavor;
                })
                .collect(Collectors.toList());
        dishFlavorMapper.saveFlavoies(flavorList);


    }

    /**
     * 菜品的分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        //分页插件
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        List<Dish> list = dishMapper.page(dishPageQueryDTO);
        PageInfo<Dish> pageInfo = new PageInfo<>(list);
        //把list换成DishVO的list
        List<DishVO> voList = list.stream().map(dish -> {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(dish, dishVO);
            //设置分类的名字
            //根据categoryId查询category表
            Category category = categoryMapper.findById(dish.getCategoryId());
            dishVO.setCategoryName(category.getName());
            return dishVO;
        }).collect(Collectors.toList());
        return new PageResult(pageInfo.getTotal(),voList);

    }

    /**
     * 根据id删除一条或者多条数据
     *
     * 核心思路
     *    什么情况下不能删除数据??
     *      1.菜品起售的时候不能是删除
     *      2.判断这个菜品是否关联了套餐
     *    核心逻辑
     *      根据id删除菜品
     *      根据dishId删除口味
     */
    @Transactional
    @Override
    public void delByIds(List<Long> ids) {
        //1.根据id查询菜品,判断状态
        ids.forEach(id->{
            //根据id查询菜品
            Dish dish = dishMapper.findById(id);
            if(dish.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        });
        //2.判断ids是否关联了套餐(可能有多个菜品)
        List<Long> setmealIds = setmealDishMapper.findSetmealIdsByDishIds(ids);
        if( setmealIds!=null && setmealIds.size()>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //根据id删除菜品
        dishMapper.delByIds(ids);
        //根据dishId删除口味
        ids.forEach(id->{
            dishFlavorMapper.delByDishId(id);
        });
    }

    /**
     * 条件查询菜品和口味
     * @return
     */
    public List<DishVO> listWithFlavor(DishDTO dishDTO) {
        List<Dish> dishList = dishMapper.findDishesByCid(dishDTO);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.findByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
